// pages/city/city.js
var QQMapWX = require('../../datas/qqmap-wx-jssdk.js')
const db = wx.cloud.database()
const MY_KEY = 'P2JBZ-LO6HJ-SVDFL-KHGTB-4ICP5-SAFXB'
var qqmapsdk = new QQMapWX({
    key: MY_KEY,
});;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        keywords: '', //输入框
        suugsetList: [], //位置
        positioning: [
            {   id:1,
                placeNames: "北京",
                Location:{
                    lng:116.41667,
                    lat:39.91667,
                }
            },
            {   id:2,
                placeNames: "上海",
                Location:{
                    lng:116.41667,
                    lat:39.91667,
                }
            },
            {   id:3,
                placeNames: "北京",
                Location:{
                    lng:114.029224829,
                    lat: 22.609581472,
                }
            },
            
        ],
        citty:[],//
        curCity:[],//
        Historical:[]//历史记录
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        db.collection('historical').get({
            success: (res)=> {
                this.setData({
                    Historical:res.data
                })
            }
           
          })

    },
    //获取定位
    positioning() {
                qqmapsdk.reverseGeocoder({
                    //成功后的回调
                    success: res => {
                        console.log(res);
                        this.setData({
                            citty:res.result
                        })
                        var curCity=this.data.citty
              
                   //开启通道
                    var eventSelect = this.getOpenerEventChannel()
                    //传递事件
                    eventSelect.emit('rcurCity', {
                        curCity:curCity
                    })
                        wx.navigateBack({
                            delta: 1,
                        })
                    }
                });

            
       
    },

    //热门城市
    beiji(){
         //获取位置
      qqmapsdk.reverseGeocoder({
        location: {
          latitude:this.data.positioning[0].Location.lat,
          longitude:this.data.positioning[0].Location.lng,
        },
        success: (res) => {
            this.setData({
                curCity:res.result  
            })
            var curCity=this.data.curCity
            console.log(this.data.curCity);
           //开启通道
           var eventSelect = this.getOpenerEventChannel()
           //传递事件
           eventSelect.emit('rcurCity', {
               curCity:curCity
           })
               wx.navigateBack({
                   delta: 1,
               })
        },
      })
    },

    //点击开启腾讯地图
    onSelectSuggsetList() {
        if (this.data.keywords != '') {
            qqmapsdk.getSuggestion({
                keyword: this.data.keywords,
                success: (res) => {

                    this.setData({
                        suugsetList: res.data
                    })

                },

            })
        } else {
            this.setData({
                suugsetList: []
            })
        }
    },

    onSuggestList(e) {
        console.log('eee',);
        //获取点击下标
        var curCityLocation = this.data.suugsetList[e.currentTarget.dataset.index]
        console.log(curCityLocation);
        //开启通道
        var eventSelect = this.getOpenerEventChannel()
        db.collection('historical').where({
            city:curCityLocation.city,
          })
          .get({
            success: function(res) {
                if (res.data.length==0) {
                    db.collection('historical').add({
                        data:curCityLocation
                    })
                }
            }
          })
        //传递事件
        eventSelect.emit('returnLocation', {
            curCityLocation: curCityLocation
        })
        //返回上一页
        wx.navigateBack({
            delta: 1,
        })
    },
    Historical(e){
        var index=e
             //开启通道
             var eventSelect = this.getOpenerEventChannel()
            //传递事件
            eventSelect.emit('index', {
                index: index
            })
            //返回上一页
            wx.navigateBack({
                delta: 1,
            })
    },
  //清楚历史记录
  istoricalRomove(){
        db.collection('historical').get({
            success: (res)=> {
                for (let i = 0; i < res.data.length; i++) {
                    db.collection('historical').doc(res.data[i]._id).remove()    
                }
                this.setData({
                    Historical:''
                })
            }
           
          })
    }
})